package org.ourguy.mymoviesandshows.adapters;

import java.util.ArrayList;

import org.ourguy.mymoviesandshows.R;
import org.ourguy.mymoviesandshows.datamodel.ITitle;
import org.ourguy.mymoviesandshows.datamodel.Series;
import org.ourguy.mymoviesandshows.managers.DataAccessManager;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Demonstrates how to write an efficient list adapter. The adapter used in this example binds
 * to an ImageView and to a TextView for each row in the list.
 *
 * To work efficiently the adapter implemented here uses two techniques:
 * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
 * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
 *
 * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
 * getView(). This data structures contains references to the views we want to bind data to, thus
 * avoiding calls to findViewById() every time getView() is invoked.
 */
public class ShowsAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater mInflater;
	private ArrayList<ITitle> titles;
	private DataAccessManager menager;
	private ITitle show;

	public ShowsAdapter(Context context, ArrayList<ITitle> titles) {
		this.context = context;
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		
		this.titles = titles;
		
		menager = DataAccessManager.getInstance((Activity)this.context);
	}

	/**
	 * The number of items in the list is determined by the number of speeches
	 * in our array.
	 *
	 * @see android.widget.ListAdapter#getCount()
	 */
	public int getCount() {
		return titles.size();
	}

	/**
	 * Since the data comes from an array, just returning the index is
	 * sufficent to get at the data. If we were using a more complex data
	 * structure, we would return whatever object represents one row in the
	 * list.
	 *
	 * @see android.widget.ListAdapter#getItem(int)
	 */
	public ITitle getItem(int position) {
		return titles.get(position);
	}

	/**
	 * Use the array index as a unique id.
	 *
	 * @see android.widget.ListAdapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}
	
	

	/**
	 * Make a view to hold each row.
	 *
	 * @see android.widget.ListAdapter#getView(int, android.view.View,
	 *	  android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		// A ViewHolder keeps references to children views to avoid unneccessary calls
		// to findViewById() on each row.
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is no need
		// to reinflate it. We only inflate a new View when the convertView supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.shows_layout, null);

			// Creates a ViewHolder and store references to the two children views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.showTitle);
			holder.airDate = (TextView) convertView.findViewById(R.id.airDate);
			holder.cover = (ImageView) convertView.findViewById(R.id.showImage);
			holder.showSelected = (CheckBox)convertView.findViewById(R.id.showSel);

			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}
		

		// Bind the data efficiently with the holder.
		show = (ITitle)getItem(position);
		holder.title.setText(show.getTitle());
		holder.airDate.setText(((Series)show).getAirDay() + " " + ((Series)show).getAirTime());
		holder.cover.setImageBitmap(show.getCoverArt().getThumbnail());
		holder.showSelected.setOnCheckedChangeListener(null);
		holder.showSelected.setChecked(show.isInFavorites());
		holder.showSelected.setTag(show);
        
		holder.showSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
				{menager.addToFavorites((ITitle)buttonView.getTag(), context);}
				else
					menager.removeFromFavorites((ITitle)buttonView.getTag(), context);
				notifyDataSetChanged();
			}
		});
		
		return convertView;
	}
	
	
	static class ViewHolder {
		TextView title;
		TextView airDate;
		ImageView cover;
		CheckBox showSelected;
	}
}